<template>
	<el-container>
<!--		<el-header style="height: auto;">-->
<!--			<sc-select-filter :data="filterData" :label-width="80" @on-change="filterChange"></sc-select-filter>-->
<!--		</el-header>-->
		<el-main class="nopadding">
			<scTable ref="table" :apiObj="apiObj" row-key="runId" stripe remoteSort remoteFilter>
				<el-table-column label="#" type="index" width="50"></el-table-column>
				<el-table-column label="流水号" prop="correlationId" width="150"></el-table-column>
				<el-table-column label="活动名称" prop="oaFinance.eventName" width="150"></el-table-column>
				<el-table-column label="活动说明" prop="oaFinance.description" width="150"></el-table-column>
				<el-table-column label="电子签名" align="center">
					<template v-slot="scope">
						<div v-if="scope.row.signatureUrl">
							<el-image
								style="width: 80px; height: 40px; cursor: pointer"
								:src="scope.row.signatureUrl"
								:preview-src-list="[scope.row.signatureUrl]"
								fit="contain"
								close-on-click-modal
								preview-teleported
								:z-index="9999"
							>
							</el-image>
						</div>
						<span v-else class="unsigned-text">未签署</span>
					</template>
				</el-table-column>
				<!--				<el-table-column label="部门" prop="deptName" width="150"></el-table-column>-->
				<el-table-column label="申请人姓名" prop="applicantName" width="200" ></el-table-column>
				<el-table-column label="申请人部门" prop="applicantDeptName" width="200"></el-table-column>
				<el-table-column label="参与人数" prop="oaFinance.participantCount" width="150"></el-table-column>
				<el-table-column label="审批人" prop="approverName" width="150"></el-table-column>
				<el-table-column label="审批人部门" prop="approverDeptName" width="150"></el-table-column>
				<el-table-column label="状态" prop="stats">
					<template #default="scope">
						<span v-if="scope.row.stats === '0'">审核中</span>
						<span v-else-if="scope.row.stats === '1'">通过</span>
						<span v-else-if="scope.row.stats === '2'">驳回</span>
					</template>
				</el-table-column>
				<el-table-column label="开始时间" prop="oaFinance.startDate" width="150" :show-overflow-tooltip="true"></el-table-column>
				<el-table-column label="结束时间" prop="oaFinance.endDate" width="150" :show-overflow-tooltip="true"></el-table-column>
				<el-table-column label="预算总金额" prop="oaFinance.totalBudget" width="150" :show-overflow-tooltip="true"></el-table-column>

				<el-table-column label="操作" fixed="right" align="right" width="100">
					<template #default="scope">
						<el-button-group>
							<el-button text type="primary" size="small" @click="readBpmDetails(scope.row,scope.$index)">查看预算明细</el-button>
						</el-button-group>
						<el-button text type="primary" size="small" v-if="scope.row.stats === '0'" @click="openApprovalDialog(scope.row)">审批</el-button>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
		<el-dialog v-model="dialogVisible" title="费用明细" width="60%">
			<el-table :data="oaFinanceDetails">
				<el-table-column label="id" prop="id" ></el-table-column>
				<el-table-column label="内容" prop="content" ></el-table-column>
				<el-table-column label="金额" prop="amount" ></el-table-column>
			</el-table>
			<template #footer>
				<el-button @click="dialogVisible = false">关闭</el-button>
			</template>
		</el-dialog>
		<!-- 新增审批弹窗 -->
		<el-dialog v-model="approvalDialogVisible" title="审批" width="40%">
			<el-form :model="approvalFormData" label-width="100px">
				<el-form-item label="审批结果">
					<el-radio-group v-model="approvalFormData.stats">
						<el-radio label="1">同意</el-radio>
						<el-radio label="2">拒绝</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="备注">
					<el-input v-model="approvalFormData.remark" type="textarea" :rows="4"></el-input>
				</el-form-item>
				<div>
					<spersonalFinance
						ref="signaturePad"
						:line-width="4"
						:line-color="'#409EFF'"
						@save="handleSave"
						@error="handleError"
						@reset="reset"
					></spersonalFinance>
				</div>
			</el-form>
			<template #footer>
				<el-button @click="approvalDialogVisible = false">取消</el-button>
				<el-button type="primary" @click="submitApproval">提交</el-button>
			</template>
		</el-dialog>
	</el-container>
</template>

<script>
import scSelectFilter from '@/components/scSelectFilter'
import spersonalFinance from '@/components/scpersonalFinance/index.vue'
import { FINANCE_CODES } from '@/config/statusCodes'

export default {
	name: 'My Attend Record',
	components: {
		scSelectFilter,
		spersonalFinance

	},
	data() {
		return {
			resultImg:'',
			approvalDialogVisible: false, // 控制审批弹窗显示
			approvalFormData: {},
			currentRow: null, // 当前行数据
			dialog: {
				save: false,
				detailsSave:false
			},
			apiObj: null,
			search: {
				date:''
			},
			dialogVisible: false, // 控制弹窗显示
			oaFinanceDetails: [], // 存储费用明细数据
			addFormVisible: false, // 控制新增表单弹窗显示
			formData: {
				oaFinance:{},
				oaFinanceDetailsList:[],
				oaFlows:[],
			}
		}
	},
	mounted() {
		this.initApiObj();
	},
	methods: {
		reset(){
			this.resultImg = ''
		},
		// 处理签名保存
		handleSave({ base64, file }) {
			// 自定义上传逻辑
			const formData = new FormData()
			formData.append('file', file)
			formData.append('code', FINANCE_CODES.ACTIVITY_FUNDING)

			this.$API.office.personalFinance.uploadFileByCode.post(formData)
				.then(res => {
					this.resultImg  = res.redirect
				})
				.catch(err => {
					this.$message.error('文件上传失败')
				})
		},

		// 处理错误
		handleError(message) {
			this.$message.error(message)
		},
		// 初始化apiObj
		initApiObj: function () {
			const userInfo = this.$TOOL.data.get("USER_INFO");
			this.apiObj = {
				get: async (params) => {
					// 合并固定参数（userInfo.id）和动态参数（this.search）
					const mergedParams = {
						option2: userInfo.id,
						...this.search,
						...params // 允许外部传入的params覆盖默认值
					};
					console.log("this.apiObj费用报销", 2222)
					return await this.$API.office.personalFinance.getByApprover.get(mergedParams);
				}
			};
		},
		filterChange(data){
			this.search.date = data.date;
			this.$refs.table.upData(this.search); // 触发表格重新加载
		},
		readBpmDetails(row,index){
			this.oaFinanceDetails = row.oaFinanceDetails;
			console.log(this.oaFinanceDetails);
			this.dialogVisible = true; // 显示弹窗
		},
		openApprovalDialog(row) {
			this.currentRow = row;
			this.approvalDialogVisible = true;
			this.approvalFormData.stats = '';
			this.approvalFormData.remark = '';
			if (this.$refs.signaturePad) {
				this.$refs.signaturePad.handleReset();
			}

		},
		submitApproval() {
			if (!this.approvalFormData.stats) {
				this.$message.error('请选择审批结果');
				return;
			}
			if (
				typeof this.resultImg === 'undefined' ||
				this.resultImg === null ||
				(typeof this.resultImg === 'string' && this.resultImg.trim() === '')
			) {
				this.$message.error('请生成签名后或字迹规范后审批');
				return;
			}
			// 提交审批逻辑
			let params = {
				correlationId: this.currentRow.correlationId,
				stats: this.approvalFormData.stats,
				remark: this.approvalFormData.remark,
				signatureUrl:this.resultImg
			};
			// this.approvalFormData.id = this.currentRow.id;
			console.log(this.approvalFormData)
			this.$API.office.personalFinance.FinancesApprove.get(params).then(res => {
				this.$refs.signaturePad.handleReset();
				this.$message.success('审批成功');
				this.approvalDialogVisible = false;
				this.initApiObj();
			}).catch(err => {
				this.$refs.signaturePad.handleReset();
				this.$message.error('审批失败');
				console.error("接口调用失败", err);
			});
		},
	}
}
</script>

<style>
</style>
